package jhunter.web.mvc;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import jhunter.rt.conf.ext.ActionExt;
import jhunter.web.mvc.params.FlashMap;

/**
 * 
 * @author jkyang
 * 
 */
public class ActionContext implements Serializable {

	private static final long serialVersionUID = -6498956208203222923L;

	private static ThreadLocal<ActionContext> action = new ThreadLocal<ActionContext>();

	public static ActionContext getContext() {
		return action.get();
	}

	public static void setContext(ActionContext context) {
		action.set(context);
	}

	private ActionExt actionConfig;
	
	private Map<String, Object> attributes;

	private Map<String, Object> parameters;

	private Map<String, Object> session;

	private Map<String, Object> application;

	private FlashMap flash;
	
	public Subject getUser(){
		return SecurityUtils.getSubject();
	}
	
	public Map<String, Object> getParameters() {
		return parameters;
	}

	public void setParameters(Map<String, Object> parameters) {
		this.parameters = parameters;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getApplication() {
		return application;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}


	public static void main(String[] args) {
		GroovyClassLoader loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
		GroovyShell shell = new GroovyShell(loader);
		ActionContext ac = new ActionContext();
		Map<String, Object> map = new HashMap<String, Object>();
		ac.setParameters(map);
		map.put("a", "testing");
		shell.setVariable("a", ac);
		Object o = shell.evaluate("a.parameters.a = 'ccc'");
		System.out.println(o);
		System.out.println(map.get("a"));
	}

	public ActionExt getActionConfig() {
		return actionConfig;
	}

	public void setActionConfig(ActionExt actionConfig) {
		this.actionConfig = actionConfig;
	}

	public Map<String, Object> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

	public FlashMap getFlash() {
		return flash;
	}

	public void setFlash(FlashMap flash) {
		this.flash = flash;
	}

	

}
